package com.yunshan.order.service.impl;

import com.yunshan.common.utils.DateUtils;
import com.yunshan.common.utils.SecurityUtils;
import com.yunshan.member.domain.MemberUserInfo;
import com.yunshan.member.service.IMemberUserInfoService;
import com.yunshan.order.domain.OrderRechargeRecord;
import com.yunshan.order.domain.OrderUniVo;
import com.yunshan.order.mapper.OrderRechargeRecordMapper;
import com.yunshan.order.service.IOrderRechargeRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * 充值订单Service业务层处理
 * 
 * @author yunshan
 * @date 2025-01-20
 */
@Service
public class OrderRechargeRecordServiceImpl implements IOrderRechargeRecordService 
{
    @Autowired
    private OrderRechargeRecordMapper orderRechargeRecordMapper;
    @Autowired
    private IMemberUserInfoService memberUserInfoService;

    /**
     * 查询充值订单
     * 
     * @param id 充值订单主键
     * @return 充值订单
     */
    @Override
    public OrderRechargeRecord selectOrderRechargeRecordById(Long id)
    {
        OrderRechargeRecord orderRechargeRecord = orderRechargeRecordMapper.selectOrderRechargeRecordById(id);
        orderRechargeRecord.setMemberNum(memberUserInfoService.selectMemberUserInfoById(orderRechargeRecord.getMemberId()).getMemberNum());
        return orderRechargeRecord;
    }

    /**
     * 查询充值订单列表
     * 
     * @param orderRechargeRecord 充值订单
     * @return 充值订单
     */
    @Override
    public List<OrderRechargeRecord> selectOrderRechargeRecordList(OrderRechargeRecord orderRechargeRecord)
    {
        if (orderRechargeRecord.getMemberId() != null){
            orderRechargeRecord.setStatus(1);
        }
        return orderRechargeRecordMapper.selectOrderRechargeRecordList(orderRechargeRecord);
    }

    /**
     * 新增充值订单
     * 
     * @param orderRechargeRecord 充值订单
     * @return 结果
     */
    @Override
    public int insertOrderRechargeRecord(OrderRechargeRecord orderRechargeRecord)
    {
        MemberUserInfo memberUserInfo = memberUserInfoService.selectMemberUserInfoById(orderRechargeRecord.getMemberId());
        orderRechargeRecord.setBalance(memberUserInfo.getBalance());
        orderRechargeRecord.setCreateBy(SecurityUtils.getUsername());
        orderRechargeRecord.setCreateTime(DateUtils.getNowDate());
        return orderRechargeRecordMapper.insertOrderRechargeRecord(orderRechargeRecord);
    }

    /**
     * 修改充值订单
     * 
     * @param orderRechargeRecord 充值订单
     * @return 结果
     */
    @Override
    public int updateOrderRechargeRecord(OrderRechargeRecord orderRechargeRecord)
    {
        orderRechargeRecord.setUpdateTime(DateUtils.getNowDate());
        return orderRechargeRecordMapper.updateOrderRechargeRecord(orderRechargeRecord);
    }

    /**
     * 批量删除充值订单
     * 
     * @param ids 需要删除的充值订单主键
     * @return 结果
     */
    @Override
    public int deleteOrderRechargeRecordByIds(Long[] ids)
    {
        return orderRechargeRecordMapper.deleteOrderRechargeRecordByIds(ids);
    }

    /**
     * 删除充值订单信息
     * 
     * @param id 充值订单主键
     * @return 结果
     */
    @Override
    public int deleteOrderRechargeRecordById(Long id)
    {
        return orderRechargeRecordMapper.deleteOrderRechargeRecordById(id);
    }

    @Override
    public int process(OrderRechargeRecord orderRechargeRecord) {

        OrderRechargeRecord orderRecharge = orderRechargeRecordMapper.selectOrderRechargeRecordById(orderRechargeRecord.getId());
        MemberUserInfo memberUserInfo = memberUserInfoService.selectMemberUserInfoById(orderRecharge.getMemberId());
        if (memberUserInfo == null){
            throw new RuntimeException("用户不存在或者已注销");
        }

        //进行充值余额
        if (orderRechargeRecord.getStatus() == 1){
            synchronized (this) {
                int i = memberUserInfoService.addMemberBalance(memberUserInfo.getId(),orderRecharge.getAmount());
                if (i < 1){
                    throw new RuntimeException("余额充值失败！");
                }
            }
            orderRechargeRecord.setBalance(memberUserInfo.getBalance().add(orderRecharge.getAmount()));
            orderRechargeRecord.setRechargeTime(DateUtils.getNowDate());

        //审核拒绝
        }else {
            orderRechargeRecord.setBalance(memberUserInfo.getBalance());
        }

        orderRechargeRecord.setUpdateBy(SecurityUtils.getUsername());
        return orderRechargeRecordMapper.updateOrderRechargeRecord(orderRechargeRecord);
    }

    @Override
    public List<OrderUniVo> selectOrderUniList(Long memberId) {
        return orderRechargeRecordMapper.selectOrderUniList(memberId);
    }
}
